package com.sokal.easycare.modules.product.web.brand;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.modules.picture.entity.PicArticleFile;
import com.sokal.easycare.modules.product.entity.brand.ProBrand;
import com.sokal.easycare.modules.product.entity.factory.ProFactory;
import com.sokal.easycare.modules.product.service.brand.ProBrandService;

/**
 * 产品品牌Controller
 * @author liliangdong
 * @version 2017-03-12
 */
@Controller
@RequestMapping(value = "${adminPath}/product/brand/proBrand")
public class ProBrandController extends BaseController {

	@Autowired
	private ProBrandService proBrandService;
	
	@ModelAttribute
	public ProBrand get(@RequestParam(required=false) String id) {
		ProBrand entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = proBrandService.get(id);
		}
		if (entity == null){
			entity = new ProBrand();
		}
		return entity;
	}
	
	@RequiresPermissions("product:brand:proBrand:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProBrand proBrand, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProBrand> page = proBrandService.findPage(new Page<ProBrand>(request, response), proBrand); 
		model.addAttribute("page", page);
		return "modules/product/brand/proBrandList";
	}

	@RequiresPermissions("product:brand:proBrand:view")
	@RequestMapping(value = "form")
	public String form(ProBrand proBrand, Model model) {
		model.addAttribute("proBrand", proBrand);
		return "modules/product/brand/proBrandForm";
	}

	@RequiresPermissions("product:brand:proBrand:edit")
	@RequestMapping(value = "save")
	public String save(ProBrand proBrand, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proBrand)){
			return form(proBrand, model);
		}
		proBrandService.save(proBrand);
		addMessage(redirectAttributes, "保存产品品牌成功");
		return "redirect:"+Global.getAdminPath()+"/product/brand/proBrand/?repage";
	}
	
	@RequiresPermissions("product:brand:proBrand:edit")
	@RequestMapping(value = "delete")
	public String delete(ProBrand proBrand, RedirectAttributes redirectAttributes) {
		proBrandService.delete(proBrand);
		addMessage(redirectAttributes, "删除产品品牌成功");
		return "redirect:"+Global.getAdminPath()+"/product/brand/proBrand/?repage";
	}
	
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ProBrand> list = proBrandService.findList(new ProBrand());
		for (int i=0; i<list.size(); i++){
			ProBrand e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	/**
	 * 上传品牌logo
	 * @param logoFile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "uploadLogoFile")
	public String uploadLogoFile(@RequestParam(value = "logoFile") MultipartFile logoFile) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			String fileUrl=FastDFSClient.uploadFile(logoFile, logoFile.getOriginalFilename());
			ProBrand proBrand = new ProBrand();
			proBrand.setLogo(fileUrl);
			proBrand.setFastdfs_file_url(Global.getConfig("fastdfs_file_url"));
			map.put("success", true);
			map.put("proBrand", proBrand);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}

}